<?php

namespace app\api\controller\web\frontend;

use app\common\controller\Api;
use think\facade\Db;


/**
 * 知识产权金融
 */
class Knowledge extends Api
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 知识产权金融列表
     */
    public function list()
    {
        // 分类
        $type = input('type') ?? '';
        $keywords = input('keywords') ?? '';
        $where = [];
        if ($type) {
            $where[] = ['type_id', '=', $type];
        }
        if ($keywords) {
            $where[] = ['title|content', 'like', '%'.$keywords.'%'];
        }

        // 加上必要条件 前端展示和未被软删除
        $where[] = ['is_showswitch','=',1];
        $where[] = ['deletetime','=',null];

        // 分页
        $page = !empty(input('page')) ? input('page') : 1;
        $page_size = !empty(input('page_size')) ? input('page_size') : 10;
        if ($page < 1) {
            $page = 1;
        }
        $start = ($page - 1) * $page_size;

        $label = Db::name('web_label')->where('id','>',0)->select()->toArray();
        $label_arr = array_column($label,'name','id');
        // $data = [];
        // 专利保险
        $list = Db::name('web_knowledge')->field('id, title, type_id, tpic_image, content')->where($where)->order('weigh desc')->limit($start, $page_size)->select()->toArray();
        foreach ($list as $key => $value) {
            $list[$key]['type_name'] = $label_arr[$value['type_id']];
            $list[$key]['content'] = strip_tags(htmlspecialchars_decode($value['content']));
        }

        $count = Db::name('web_knowledge')->where($where)->count();
        
        $data = [
            'page'=>$page,
            'page_size'=>$page_size,
            'count'=>$count,
            'list'=>$list,
        ]; 

        $this->success('success',$data);
    }


    /**
     * 知识产权金融详情
     */
    public function info()
    {
        $id = input('knowledge_id');
        if (!$id) {
            return ajaxReturn('请求参数出错', 0);
        }

        $data = Db::name('web_knowledge')->find($id);
        if (empty($data)) {
            return ajaxReturn('不存在的内容', 0);
        }
        $label = Db::name('web_label')->where('id','>',0)->select()->toArray();
        $label_arr = array_column($label,'name','id');

        // 数据处理
        $data['type_name'] = $label_arr[$data['type_id']];
        $data['createtime'] = date('Y-m-d', $data['createtime']);
        $data['content'] = htmlspecialchars_decode($data['content']);

        // 上一篇 下一篇
        $data['prev'] = Db::name('web_knowledge')->where('id', '<', $id)->max('id');
        $data['next'] = Db::name('web_knowledge')->where('id', '>', $id)->min('id');

        $this->success('success',$data);
    }


}
